/**
 * Created : Mar 23, 2012
 *
 * @author pquiring
 */

//  /mnt/d/downloads/iso/DellWinXP_Pro_SP3.iso

import java.awt.*;
import javaforce.*;

public class BurnProgressDialog extends javax.swing.JDialog implements ShellProcessListener {

  /**
   * Creates new form BurnProgressDialog
   */
  public BurnProgressDialog(java.awt.Frame parent, boolean modal, String incmd[]) {
    super(parent, modal);
    initComponents();
    setPosition();
    this.cmd = incmd;
    sp.addListener(this);
//    for(int a=0;a<incmd.length;a++) {
//      System.out.println("cmd[]=" + incmd[a]);  //test
//    }
//    sp.log = true;
    thread = new Thread() {
      public void run() {
        String output = sp.run(cmd, true);
        //TODO - handle errors?
        cancel.setEnabled(false);
        close.setEnabled(true);
        if (abort) {
          progress.setValue(0);
          status.setText("Burning aborted!");
        } else {
          progress.setValue(100);
          status.setText("Burning complete!");
        }
      }
    };
    thread.start();
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        progress = new javax.swing.JProgressBar();
        close = new javax.swing.JButton();
        cancel = new javax.swing.JButton();
        status = new javax.swing.JLabel();
        speed = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Burning Disc");
        setResizable(false);

        close.setText("Close");
        close.setEnabled(false);
        close.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeActionPerformed(evt);
            }
        });

        cancel.setText("Cancel");
        cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        status.setText("Burning to disc...");

        speed.setText("Actual Write Speed: ?x");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(speed)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(cancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(close))
                    .addComponent(progress, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 485, Short.MAX_VALUE)
                    .addComponent(status, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(status)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(progress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(close)
                    .addComponent(cancel)
                    .addComponent(speed))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

  private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
    if (JFAWT.showConfirm("Confim", "Stop burning disc?")) {
      abort = true;
      status.setText("Aborting...");
      sp.destroy();
    }
  }//GEN-LAST:event_cancelActionPerformed

  private void closeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeActionPerformed
    dispose();
  }//GEN-LAST:event_closeActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancel;
    private javax.swing.JButton close;
    private javax.swing.JProgressBar progress;
    private javax.swing.JLabel speed;
    private javax.swing.JLabel status;
    // End of variables declaration//GEN-END:variables

  private String cmd[];
  private Thread thread;
  private ShellProcess sp = new ShellProcess();
  private boolean abort = false;

  public void shellProcessOutput(String string) {
    //sample output = Track 01:   59 of  212 MB written (fifo 100%) [buf  97%]  16.0x.
    int i1 = string.indexOf(":");
    int i2 = string.indexOf("of");
    int i3 = string.indexOf("KB");
    if (i3 == -1) i3 = string.indexOf("MB");
    if (i3 == -1) i3 = string.indexOf("GB");
//    System.out.println("i=" + i1 + ";" + i2 + ";" + i3);  //test
    if ((i1 == -1) || (i2 == -1) || (i3 == -1)) return;
    String s1 = string.substring(i1+1, i2).trim();
    String s2 = string.substring(i2+2, i3).trim();
    int v1 = JF.atoi(s1);
    int v2 = JF.atoi(s2);
    int value = v1 * 100 / v2;
    progress.setValue(value);
    int i4 = string.indexOf("]");
    if (i4 == -1) return;
    String s3 = string.substring(i4+1).trim();
    speed.setText("Actual Write Speed:" + s3);
  }

  private void setPosition() {
    Rectangle s = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
    Dimension d = getPreferredSize();
    setLocation(s.width/2 - d.width/2, s.height/2 - (d.height/2));
  }
}
